﻿using System;
using System.Data;
using System.Collections.Generic;
using SF.Common;
using SF.Model;
namespace SF.BLL
{
	/// <summary>
	/// 客户信息
	/// </summary>
	public partial class sf_user_info
	{
		private readonly SF.DAL.sf_user_info dal=new SF.DAL.sf_user_info();
		public sf_user_info()
		{}
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            return dal.Exists(id);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByKey(string inviteKey)
        {
            return dal.ExistsByKey(inviteKey);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByNum(string userNum)
        {
            return dal.ExistsByNum(userNum);
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByOpenId(string openid)
        {
            return dal.ExistsByOpenId(openid);
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(SF.Model.sf_user_info model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_user_info model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_user_info GetModel(long id)
        {

            return dal.GetModel(id);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.sf_user_info GetModelByNum(string userNum,string where)
        {
            return dal.GetModelByNum(userNum, where);
        }


		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SF.Model.sf_user_info> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<SF.Model.sf_user_info> DataTableToList(DataTable dt)
		{
			List<SF.Model.sf_user_info> modelList = new List<SF.Model.sf_user_info>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				SF.Model.sf_user_info model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 根据openid 返回一个实体
        /// </summary>
        public Model.sf_user_info GetModelByOpenId(string openid)
        {
            return dal.GetModelByOpenId(openid);
        }


        /// <summary>
        /// 获取 第二级 客户
        /// </summary>
        /// <param name="inviteKey">邀请码</param>
        public DataTable getTwoUsers(string inviteKey,string where)
        {
            DataTable dt = GetList(" fatherKey='" + inviteKey + "' and " + where).Tables[0];
            DataTable ds = GetList("1!=1").Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                DataTable ts = GetList(" fatherKey='" + row["inviteKey"] + "' and " + where).Tables[0];
                foreach (DataRow r in ts.Rows)
                {
                    DataRow ro = ds.NewRow();
                    ro.ItemArray = r.ItemArray;
                    ds.Rows.Add(ro);
                }
            }
            return ds;
        }

        /// <summary>
        /// 获取 第三级 客户
        /// </summary>
        /// <param name="inviteKey">邀请码</param>
        public DataTable getThreeUsers(string inviteKey,string where)
        {
            DataTable ds = GetList("1!=1").Tables[0];
            DataTable dt = getTwoUsers(inviteKey, where);
            foreach (DataRow row in dt.Rows)
            {
                DataTable ts = GetList(" fatherKey='" + row["inviteKey"] + "' and " + where).Tables[0];
                foreach (DataRow r in ts.Rows)
                {
                    DataRow ro = ds.NewRow();
                    ro.ItemArray = r.ItemArray;
                    ds.Rows.Add(ro);
                }
            }
            return ds;
        }

        /// <summary>
        /// 获取排行榜集合
        /// </summary>
        /// <returns></returns>
        public List<Model.sf_user_info> getTopList(string where)
        {
            List<Model.sf_user_info> userList = new List<Model.sf_user_info>();
            BLL.sf_goods_order orderBll = new BLL.sf_goods_order();
            DataTable dt = dal.GetList(where).Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                Model.sf_user_info model = dal.GetModel(Convert.ToInt32(row["id"]));
                decimal money = 0;
                int count = 0;
                string str = "";
                DataTable odt = orderBll.getLevelOrders(row["inviteKey"].ToString(), 4,where);
                foreach (DataRow r in odt.Rows)
                {
                    money += Convert.ToDecimal(r["totalMoney"]);
                    if (str.IndexOf(r["userNum"].ToString()) < 0)
                    {
                        str += r["userNum"];
                        count++;
                    }
                }
                model.province = money.ToString() ;
                model.city = count.ToString();
                if (userList.Count < 1)
                {
                    userList.Add(model);
                }
                else
                {
                    for (int i = 0; i < userList.Count; i++)
                    {
                        if (Convert.ToDecimal(model.province) > Convert.ToDecimal(userList[i].province))
                        {
                            userList.Insert(i, model);
                            break;
                        }
                        else
                        {
                            userList.Add(model);
                            break;
                        }
                    }
                }
            }
            return userList;

        }

        /// <summary>
        /// 根据邀请码 返回一个实体
        /// </summary>
        public Model.sf_user_info GetModelByKey(string inviteKey,string where)
        {
            return dal.GetModelByKey(inviteKey,where);
        }
        /// <summary>
        /// 关注时 获取当前 用户数量
        /// </summary>
        public long getUserCount(string where)
        {
            return dal.getUserCount(where);
        }
        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string strValue, string where)
        {
            return dal.UpdateField(strValue, where);
        }
		#endregion  ExtensionMethod
	}
}

